package com.jse.filter;

import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.ActionContext;
import org.nutz.mvc.ActionFilter;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.View;
import org.nutz.mvc.impl.processor.AbstractProcessor;

public class JseActionFilter extends AbstractProcessor implements ActionFilter {

	Log log = Logs.get();

	PropertiesProxy conf = Mvcs.getIoc().get(PropertiesProxy.class, "conf");

	@Override
	public View match(ActionContext ac) {
		if (conf.getBoolean("mvc.filter")) {
			try {
				return ActionFilter.class.cast(Class.forName(conf.get("mvc.class")).newInstance()).match(ac);
			} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
				log.error(e);
			}
		}
		return null;
	}

	@Override
	public void process(ActionContext ac) throws Throwable {
		if (conf.getBoolean("mvc.aop")) {
			AbstractProcessor.class.cast(Class.forName(conf.get("mvc.class")).newInstance()).process(ac);
		} else {
			doNext(ac);
		}
	}

}
